﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using Shell32;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Screenshot
{
    public class Window
    {


        public static Bitmap Snap( string windowTitleName )
        {
            return Snap( windowTitleName, null );
        }

        public static Bitmap Snap( string windowTitleName, string windowClassName )
        {
            IntPtr windowHandle = Win32API.FindWindow( windowClassName, windowTitleName );
            return Snap( windowHandle );
        }


        //public static Bitmap Snap2( IntPtr windowHandle )
        //{
        //    Screen scr = Screen.FromHandle( windowHandle );
        //       D

        //    int screenWidth = scr.Bounds.Width;
        //    int screenHeight = scr.Bounds.Height;
        //    Bitmap img = new Bitmap( screenWidth, screenHeight );

        //    Graphics gfx = Graphics.FromImage( (Image)img );
        //    gfx.CopyFromScreen( 0, 0, 0, 0, new Size( screenWidth, screenHeight ) );

        //    return img;
        //}

        public static Bitmap Snap( IntPtr windowHandle )
        {
            if ( Win32API.IsValidWindowHandle( windowHandle ) )
            {

                if ( Win32API.IsIconic( windowHandle ) )
                {
                    Win32API.ShowWindow( windowHandle, Win32API.WindowShowStyle.Restore );
                }

                IntPtr hDC = Win32API.GetWindowDC( windowHandle );
                IntPtr hMemDC = Win32API.CreateCompatibleDC( hDC );

                Win32API.RECT rec;
                Win32API.GetClientRect( windowHandle, out rec );

                int width = ( rec.right - rec.left );
                int height = ( rec.bottom - rec.top );

                IntPtr m_HBitmap = Win32API.CreateCompatibleBitmap( hDC, width, height );

                if ( m_HBitmap != IntPtr.Zero )
                {
                    IntPtr hOld = (IntPtr)Win32API.SelectObject( hMemDC, m_HBitmap );
                    Win32API.BitBlt( hMemDC, 0, 0, width, height, hDC, 0, 0, Win32API.SRCCOPY );
                    Win32API.SelectObject( hMemDC, hOld );
                    Win32API.DeleteDC( hMemDC );
                    Win32API.ReleaseDC( windowHandle, hDC );
                    return System.Drawing.Image.FromHbitmap( m_HBitmap );
                }
            }
            return null;
        }

        public static Bitmap SnapByClassName( string windowClassName )
        {
            return Snap( null, windowClassName );
        }

        public static Bitmap Snap()
        {
            return Snap( Win32API.GetDesktopWindow() );
        }

        // That method sucks ... need to find a way to do it without minimizing
        public static Bitmap SnapDesktop()
        {
            Shell objShel = new Shell();

            // Show the desktop
            ( (Shell32.IShellDispatch4)objShel ).ToggleDesktop();

            System.Threading.SpinWait.SpinUntil( () => false, 500 );
            Bitmap img = Snap();

            // Restore the desktop
            ( (Shell32.IShellDispatch4)objShel ).ToggleDesktop();

            return img;
        }
    }
}
